﻿using System;
using NUnit.Framework;
using Rhino.Mocks;
using System.Linq;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Globalization;
using System.Collections.Specialized;

namespace NMA.Infrastructure.NHibernate.Tests.Facade
{
    using NMA.Domain.Model.Repository;
    using NMA.Infrastructure.NHibernate.Service.Event;
    using NMA.Infrastructure.EventAggregator;
    using NMA.Application;
    using NMA.Domain.Shared;
    using NMA.Domain.Model;
    using NMA.Infrastructure.NHibernate.DomainObject;
    using NMA.Domain.Model.Service.Application;
    using NMA.Domain.Model.DTO;
    using NMA.Domain.Shared.Paging;
    using NMA.Infrastructure.NHibernate.Paging;

    [TestFixture]
    public class ResourceFacadeTest : TestingBase
    {
        private IResourceRepository _resourceRepository = null;
        private IConsumer<AuditEvent> _handler = null;
        private IResourceFacade _resourceFacade = null;
        private IDomainObjectFactory _objectfactory = null;
        private IAuditFactory _auditFactory = null;

        private const string RESOURCE_TYPE = "resource type 1";

        protected override void Before_each_test()
        {
            base.Before_each_test();

            _resourceRepository = MockRepository.GenerateMock<IResourceRepository>();
            _handler = MockRepository.GenerateMock<IConsumer<AuditEvent>>();
            _objectfactory = new DomainObjectFactory();
            _auditFactory = MockRepository.GenerateMock<IAuditFactory>();
            _resourceFacade = new ResourceFacade(_resourceRepository, _handler, _auditFactory);
            _resourceFacade.ResourceType = RESOURCE_TYPE;
        }

        [Test]
        public void Can_call_get_resource_by_culture_and_key_internal()
        {
            // Arrange
            IEnumerable<IResources> list = new List<IResources>() { new Resources() {
                 Id = Guid.NewGuid(),
                 ResourceType = RESOURCE_TYPE,
                 CultureCode = "en-GB",
                 ResourceKey = "resource key 1",
                 ResourceValue = "resource value 1",
                 CreatedDate = DateTime.Now,
                 CreatedUser = "user test",
                 UpdatedDate = DateTime.Now,
                 UpdatedUser = "user test"
            }};

            CultureInfo culture = new CultureInfo("en-GB");

            _resourceRepository.Stub(c => c.GetBy(Arg<Expression<Func<IResources, bool>>>.Is.NotNull)).Return(list);
            // Act
            var result = _resourceFacade.GetResourceByCultureAndKeyInternal(culture, "resource key 1");
            // Assert
            Assert.IsNotNull(result);
            Assert.IsTrue(string.Compare(result, "resource value 1") == 0);
        }

        [Test]
        public void Can_call_get_resources_by_culture()
        {
            // Arrange
            IEnumerable<IResources> list = new List<IResources>() { new Resources() {
                 Id = Guid.NewGuid(),
                 ResourceType = RESOURCE_TYPE,
                 CultureCode = "en-GB",
                 ResourceKey = "resource key 1",
                 ResourceValue = "resource value 1",
                 CreatedDate = DateTime.Now,
                 CreatedUser = "user test",
                 UpdatedDate = DateTime.Now,
                 UpdatedUser = "user test"
            }};

            CultureInfo culture = new CultureInfo("en-GB");

            _resourceRepository.Stub(c => c.GetBy(Arg<Expression<Func<IResources, bool>>>.Is.NotNull)).Return(list);
            // Act
            var result = _resourceFacade.GetResourcesByCulture(culture);
            // Assert
            Assert.IsNotNull(result);
            Assert.IsTrue(result.Keys.Count == 1);
            Assert.IsTrue(result.Values.Count == 1);
        }

        [Test]
        public void Can_call_get_resource_by_culture_and_key()
        {
            // Arrange
            IEnumerable<IResources> list = new List<IResources>() { new Resources() {
                 Id = Guid.NewGuid(),
                 ResourceType = RESOURCE_TYPE,
                 CultureCode = "en-GB",
                 ResourceKey = "resource key 1",
                 ResourceValue = "resource value 1",
                 CreatedDate = DateTime.Now,
                 CreatedUser = "user test",
                 UpdatedDate = DateTime.Now,
                 UpdatedUser = "user test"
            }};

            CultureInfo culture = new CultureInfo("en-GB");

            _resourceRepository.Stub(c => c.GetBy(Arg<Expression<Func<IResources, bool>>>.Is.NotNull)).Return(list);
            // Act
            var result = _resourceFacade.GetResourceByCultureAndKey(culture, "resource key 1");
            // Assert
            Assert.IsNotNull(result);
            Assert.IsTrue(string.Compare(result, "resource value 1") == 0);
        }

        protected override void After_each_test()
        {
            base.After_each_test();

            if (_resourceRepository != null)
                GC.SuppressFinalize(_resourceRepository);
            if (_handler != null)
                GC.SuppressFinalize(_handler);
            if (_resourceFacade != null)
                GC.SuppressFinalize(_resourceFacade);
            if (_objectfactory != null)
                GC.SuppressFinalize(_objectfactory);
        }
    }
}